<!DOCTYPE html>
<html>
<head>
    <title>Acoustic Impedance Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="900" height="500" style="border:1px solid #000;"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Clear canvas
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // --- Draw Medium B ---
    // Create a subtle gradient for the medium
    const gradient = ctx.createLinearGradient(450, 0, 900, 0);
    gradient.addColorStop(0, '#EAEAEA');
    gradient.addColorStop(1, '#DCDCDC');
    ctx.fillStyle = gradient;
    ctx.fillRect(450, 0, 450, canvas.height);

    // --- Draw Boundary Line ---
    ctx.beginPath();
    ctx.moveTo(450, 0);
    ctx.lineTo(450, canvas.height);
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 1.5;
    ctx.stroke();

    // --- Helper function to draw an arrow ---
    function drawArrow(ctx, fromx, fromy, tox, toy) {
        const headlen = 15; // length of head in pixels
        const dx = tox - fromx;
        const dy = toy - fromy;
        const angle = Math.atan2(dy, dx);
        ctx.beginPath();
        ctx.moveTo(fromx, fromy);
        ctx.lineTo(tox, toy);
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
        ctx.stroke();
    }
    
    // --- Draw Text Labels ---
    ctx.fillStyle = 'black';
    const baseFont = '24px sans-serif';
    const symbolFont = 'italic 24px serif';
    const subFont = '18px sans-serif';

    // --- Medium A Labels (Left Side) ---
    ctx.textAlign = 'center';
    ctx.font = baseFont;
    ctx.fillText('medium A', 225, 60);

    // Draw "specific acoustic impedance ZA" centered at x=225, y=90
    const text_specific = 'specific acoustic impedance ';
    const text_Z = 'Z';
    const text_A = 'A';
    
    ctx.font = baseFont;
    const w_specific = ctx.measureText(text_specific).width;
    ctx.font = symbolFont;
    const w_Z = ctx.measureText(text_Z).width;
    ctx.font = subFont;
    const w_A = ctx.measureText(text_A).width;
    
    const total_width_A = w_specific + w_Z + w_A;
    let currentX_A = 225 - total_width_A / 2;

    ctx.font = baseFont;
    ctx.fillText(text_specific, currentX_A + w_specific / 2, 90);
    currentX_A += w_specific;

    ctx.font = symbolFont;
    ctx.fillText(text_Z, currentX_A, 90);
    currentX_A += w_Z;

    ctx.font = subFont;
    ctx.fillText(text_A, currentX_A, 90 + 6);
    

    // --- Medium B Labels (Right Side) ---
    ctx.textAlign = 'center';
    ctx.font = baseFont;
    ctx.fillText('medium B', 675, 60);

    // Draw "specific acoustic impedance ZB" centered at x=675, y=90
    const text_B = 'B';
    ctx.font = subFont;
    const w_B = ctx.measureText(text_B).width;

    const total_width_B = w_specific + w_Z + w_B;
    let currentX_B = 675 - total_width_B / 2;

    ctx.font = baseFont;
    ctx.fillText(text_specific, currentX_B + w_specific / 2, 90);
    currentX_B += w_specific;

    ctx.font = symbolFont;
    ctx.fillText(text_Z, currentX_B, 90);
    currentX_B += w_Z;

    ctx.font = subFont;
    ctx.fillText(text_B, currentX_B, 90 + 6);


    // --- Draw Beam Arrows ---
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 2;
    const arrowY = [160, 210, 260];
    
    // Incident Arrows
    arrowY.forEach(y => {
        drawArrow(ctx, 50, y, 440, y);
    });

    // Transmitted Arrows
    arrowY.forEach(y => {
        drawArrow(ctx, 460, y, 850, y);
    });

    // --- Draw Beam Property Labels ---
    ctx.textAlign = 'left';

    // Incident Beam Labels
    ctx.font = baseFont;
    ctx.fillText('incident', 50, 320);
    
    // Draw "intensity I0" at x=50, y=350
    const text_intensity = 'intensity ';
    const text_I = 'I';
    const text_0 = '0';
    let currentX_I0 = 50;

    ctx.font = baseFont;
    ctx.fillText(text_intensity, currentX_I0, 350);
    currentX_I0 += ctx.measureText(text_intensity).width;

    ctx.font = symbolFont;
    ctx.fillText(text_I, currentX_I0, 350);
    currentX_I0 += ctx.measureText(text_I).width;

    ctx.font = subFont;
    ctx.fillText(text_0, currentX_I0, 350 + 6);


    // Transmitted Beam Labels
    ctx.font = baseFont;
    ctx.fillText('transmitted', 500, 320);

    // Draw "intensity IT" at x=500, y=350
    const text_T = 'T';
    let currentX_IT = 500;

    ctx.font = baseFont;
    ctx.fillText(text_intensity, currentX_IT, 350);
    currentX_IT += ctx.measureText(text_intensity).width;
    
    ctx.font = symbolFont;
    ctx.fillText(text_I, currentX_IT, 350);
    currentX_IT += ctx.measureText(text_I).width;
    
    ctx.font = subFont;
    ctx.fillText(text_T, currentX_IT, 350 + 6);


    // --- Draw Figure Caption ---
    ctx.textAlign = 'center';
    ctx.font = '22px sans-serif';
    ctx.fillText('Fig. 4.1', 450, 450);

</script>
</body>
</html>